package com.itaem.community.core.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itaem.community.modules.infrastructure.authority.entity.role.RoleEntity;
import com.itaem.community.modules.infrastructure.authority.entity.role.RolePermissionEntity;
import com.itaem.community.modules.infrastructure.authority.service.permission.PermissionService;
import com.itaem.community.modules.infrastructure.authority.service.role.RolePermissionService;
import com.itaem.community.modules.infrastructure.authority.service.role.RoleService;
import com.itaem.community.modules.infrastructure.user.entity.UserEntity;
import com.itaem.community.modules.infrastructure.user.entity.UserPhoneEntity;
import com.itaem.community.modules.infrastructure.user.service.UserPhoneService;
import com.itaem.community.modules.infrastructure.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lenovo
 */
@Slf4j
public class CustomRealm extends AuthorizingRealm {

    @Resource
    @Lazy
    private UserService userService;

    @Autowired
    private UserPhoneService userPhoneService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    public CustomRealm() {
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录手机号
        String phoneNumber = (String) principalCollection.getPrimaryPrincipal();
        UserPhoneEntity userPhoneEntity = userPhoneService.getOne(
                new QueryWrapper<UserPhoneEntity>().eq("phone_number",phoneNumber));
        UserEntity user = userService.getById(userPhoneEntity.getUserId());
        //获取角色
        RoleEntity role = roleService.getById(user.getRoleId());
        List<RolePermissionEntity> rolePermissionEntities = rolePermissionService.list(
                new QueryWrapper<RolePermissionEntity>().eq("role_id",role.getId())
        );
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //角色
        simpleAuthorizationInfo.addRole(role.getRoleName());
        for(RolePermissionEntity rolePermissionEntity1: rolePermissionEntities){
            //添加权限
            simpleAuthorizationInfo.addStringPermission(
                    permissionService.getById(rolePermissionEntity1.getPermissionId()).getContent());
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if(StringUtils.isEmpty(authenticationToken.getPrincipal())){
            return null;
        }
        //获取用户信息
        String phone = authenticationToken.getPrincipal().toString();
        log.info(phone);
        UserPhoneEntity userPhoneEntity = userPhoneService.getOne(
                new QueryWrapper<UserPhoneEntity>().eq("phone_number",phone));
        log.info(String.valueOf(userPhoneEntity));
        if(userPhoneEntity == null){
            return null;
        }else{
            //验证authenticationToken和simpleAuthorizationInfo的信息
            return new SimpleAuthenticationInfo(
                    phone,
                    userPhoneEntity.getPassword(),
                    getName());
        }
    }
}
